package com.itcast.springboot3x.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用Spring-Boot-Actuator发布Endpoint，用来暴露当前应用中所有线程池的Metric数据
 * @author zhangtian105
 * @date 2023/5/18
 * @apiNote
 */
@Configuration
@Endpoint(id="thread-pool")
public class ThreadPoolEndpoint {
    @Autowired
    private ThreadPoolForMonitorManager threadPoolForMonitorManager;

    /**
     * 获取线程池信息
     * @return
     */
    @ReadOperation
    public Map<String,Object> threadPoolsMetric(){
        Map<String,Object> metricMap = new HashMap<>();
        List<Map<String, Object>> threadPools = new ArrayList<>();
        threadPoolForMonitorManager.getThreadPoolMap().forEach((k, v) -> {
            ThreadPoolExecutorForMonitor tpe= v;
            Map<String,Object> poolInfo = new HashMap<>();
            poolInfo.put("thread.pool.name", k);
            poolInfo.put("thread.pool.core.size", tpe.getCorePoolSize());
            poolInfo.put("thread.pool.largest.size", tpe.getLargestPoolSize());
            poolInfo.put("thread.pool.max.size", tpe.getMaximumPoolSize());
            poolInfo.put("thread.pool.thread.count", tpe.getPoolSize());
            poolInfo.put("thread.pool.max.costTime", tpe.getMaxCostTime());
            poolInfo.put("thread.pool.average.costTime", tpe.getAverageCostTime());
            poolInfo.put("thread.pool.min.costTime", tpe.getMinCostTime());
            poolInfo.put("thread.pool.active.count", tpe.getActiveCount());
            poolInfo.put("thread.pool.completed.taskCount", tpe.getCompletedTaskCount());
            poolInfo.put("thread.pool.queue.name", tpe.getQueue().getClass().getName());
            poolInfo.put("thread.pool.rejected.name", tpe.getRejectedExecutionHandler().getClass().getName());
            poolInfo.put("thread.pool.task.count", tpe.getTaskCount());

            threadPools.add(poolInfo);
        });

        metricMap.put("threadPools", threadPools);

        return metricMap;
    }
}
